package com.itheima.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;
@Component
@Aspect
//通知类，告诉spring在增强的前后需要做什么事
public class Advice {
    @Pointcut("execution(* com.itheima.service.StudentService.*(..))")
    public void pt(){}
    @Before("pt()")
    public void before() {
        //前置通知：开启事务
        System.out.println("前置通知：开启事务");
    }
@AfterReturning("pt()")
    public void afterReturn() {
        //后置通知：提交事务
        System.out.println("后置通知：提交事务");
    }
@AfterThrowing("pt()")
    public void afterThrowable() {
        //异常通知：回滚事务
        System.out.println("异常通知：回滚事务");
    }
@After("pt()")
    public void after() {
        //最终通知：释放资源
        System.out.println("最终通知：释放资源");
    }

    public Object around(ProceedingJoinPoint pjp) {
        Object value = null;
        try {
            before();
            Object[] args = pjp.getArgs();
            System.out.println(Arrays.toString(args));
            value = pjp.proceed(args);
            afterReturn();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            afterThrowable();
        } finally {
            after();
        }
        return value;
    }
}
